import 'package:flutter/material.dart';
import 'package:mjpeg_view/mjpeg_view.dart';


class FromResourcePage extends StatefulWidget {
  FromResourcePage({Key key}) : super(key: key);

  @override
  _FromResourcePageState createState() => _FromResourcePageState();
}

class _FromResourcePageState extends State<FromResourcePage> {
  List<MjpegController> controllers;
  bool isLive = true;

  @override
  void initState() {
    super.initState();
    this.controllers = [
      new MjpegController.fromAsset(sourceName: "assets/mjpeg/demo_gradual.avi", fps: 50),
      new MjpegController.fromAsset(sourceName: "assets/mjpeg/demo_rhombus.avi", fps: 20, isLoop: false),
      new MjpegController.fromAsset(sourceName: "assets/mjpeg/demo_stripe.avi", fps: 25),
    ];
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text("MJPEG Stream From Resource")),
      body: Center(
        child: Row(
          children: List.generate(this.controllers.length, (index) => Expanded(
            child: Container(
              child: MjpegView(
                controller: this.controllers[index],
                fit: BoxFit.fill
              ),
              margin: EdgeInsets.all(5)
            )
          ))
        )
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () => this.setState(() => this.isLive = !this.isLive),
        tooltip: 'Pause',
        child: Icon(this.isLive ? Icons.pause: Icons.play_arrow)
      ),
    );
  }
}
